﻿using IcsSEMCtrl.Service.Logger;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IcsSEMCtrl.Service
{
	public class PlaybackManager
	{
		public class RequestState
		{
			private const int BufferSize = 1024;
			public byte[] BufferRead;
			public Stream ResponseStream;
			public long downBytes = 0L;
			public FileStream fs = null;
			public string useFTP = string.Empty;
			public RequestState()
			{
				this.BufferRead = new byte[1024];
				this.ResponseStream = null;
			}
		}
		public enum DownloadStatusType
		{
			None,
			Start,
			Downloading,
			Downloaded,
			Complete,
			Abort,
			Error
		}
		public class DownloadStatusArgs : EventArgs
		{
			public PlaybackManager.DownloadStatusType Status = PlaybackManager.DownloadStatusType.None;
			public int DownFileNumber = 0;  //当前下载的文件数
			public int DestFileNumber = 0;  //需要下载的文件总数
			public int DownTotalSize = 0;   //当前下载的文件Size
			public int DestTotalSize = 0;   //需要下载的文件总Size


			public DownloadStatusArgs(PlaybackManager.DownloadStatusType statusType, int downFileNumber, long downTotalSize, int destFileNumber, long destTotalSize)
			{
				this.Status = statusType;
				this.DownFileNumber = downFileNumber;
				this.DestFileNumber = destFileNumber;

				this.DownTotalSize = (int)(downTotalSize / 1024L);
				this.DestTotalSize = (int)(destTotalSize / 1024L);
			}
		}
		public enum MonthType
		{
			Jan = 1,
			Feb,
			Mar,
			Apr,
			May,
			Jun,
			Jul,
			Aug,
			Sep,
			Oct,
			Nov,
			Dec
		}
		public class localLogFileInfo
		{
			public string filePath = string.Empty;  //唯一属性
			public int m_Hour = -1;
			public localLogFileInfo(string path)
			{
				this.filePath = path;
			}
		}
		public class LogDataInfo
		{
			public DateTime time = DateTime.Now;
			public int fileHour = -1;
			public long pos = 0L;
			public int byteLen = 0;
		}
		public class XmlItem
		{
			public string index = string.Empty;
			public string datetime = string.Empty;
			public string data = string.Empty;
		}
		public const int C_PageSize = 15;
		private const int BUFFER_SIZE = 1024;
		private const int C_MaxXmlLineLen = 102400;
		private string m_useFTP = string.Empty;
		private string m_ServerURI = string.Empty;
		private string m_User = string.Empty;
		private string m_Pass = string.Empty;
		private string m_LogDate = string.Empty;
		private int m_FromHour = 0;
		private int m_ToHour = 0;
		public object SyncObject = new object();
		private string m_LocalDir = string.Empty;

		private bool bCancel = false;

		//远程Log File的下载进度
		private int destFileNumber = 0;     //Log File要下载的文件总数
		private int downFileNumber = 0;     //Log File已下载的文件数量
		private long destTotalSize = 0L;     //Log File要下载的文件总大小
		private long downTotalSize = 0L;     //Log File已下载的文件大小
		private int curFileIndex = 0;        //远程下载的Log File的当前序号

		private Encoding enckr = Encoding.GetEncoding("UTF-8");//ks_c_5601-1987
	
		private List<localLogFileInfo> localLogFileList = new List<localLogFileInfo>();  //本地Log File文件清单，用于读取
		private List<LogDataInfo> logDataList = new List<LogDataInfo>();       //Log Data的数据标签（非数据）
		public int logCount = 0;
		private byte[] xmlbuf = new byte[102400];
		private DataTable dtList = new DataTable();  //加载的Log数据DataTable

		public Action<DownloadStatusArgs> DownloadStatusChange;
		public Queue<string> stringQueue = new Queue<string>();
		public DataTable DataList
		{
			get
			{
				return this.dtList;

			}
		}
		public List<localLogFileInfo> LogFileList
		{
			get
			{
				return this.localLogFileList.OrderBy(k => k.m_Hour).ToList();

			}
		}

		public PlaybackManager()
		{
			this.InitialDataTable();
		}




		//按byte[]数组读取（全部）本地Log File -> 生成Log Data数据标签
		public void GenerateLogData1(string useFTP, string logDate, string fromTime, string toTime, string localDir)
		{
			this.logCount = 0;
			try
			{
				this.m_LogDate = logDate;
				this.m_FromHour = Convert.ToInt32(fromTime);
				this.m_ToHour = Convert.ToInt32(toTime);
				this.m_LocalDir = localDir;
				this.logDataList.Clear();
				Cursor.Current = Cursors.WaitCursor;
				this.getLocalLogFile(useFTP);
				bool flag = this.localLogFileList.Count == 0;
				if (!flag)
				{
					byte[] array = new byte[102400];
					byte[] array2 = new byte[102400];
					int num = 0;
					string text = string.Empty;
					int num6;
					for (int i = 0; i < this.localLogFileList.Count; i = num6 + 1)
					{
						PlaybackManager.localLogFileInfo localLogFileInfo = (PlaybackManager.localLogFileInfo)this.localLogFileList[i];
						long num2 = 0L;
						string str = string.Empty;
						string text2 = string.Empty;
						using (FileStream fileStream = File.Open(localLogFileInfo.filePath, FileMode.Open, FileAccess.Read))
						{
							while (fileStream.Position < fileStream.Length)
							{
								int num3 = fileStream.Read(array, 0, array.Length);
								bool flag2 = num3 > 0;
								if (!flag2)
								{
									break;
								}

								int num4 = 0;
								int num5 = 0;
								for (int j = 0; j < num3; j = num6 + 1)
								{
									bool flag3 = j == 0 && array[j] == 10;
									if (flag3)
									{
										num4 = j + 1;
									}
									else
									{
										bool flag4 = array[j] == 13;
										if (flag4)
										{
											str = string.Empty;
											bool flag5 = num5 > 0;
											if (flag5)
											{
												str = this.enckr.GetString(array, num4, num5);
											}
											text2 = text + str;
											bool flag6 = text2.Length > 0;
											if (flag6)
											{
												object eventTime = this.getEventTime(text2);
												bool flag7 = eventTime != null;
												if (flag7)
												{
													this.addLogData(text2, i, num2 + (long)num4 - (long)num, num5 + num, (DateTime)eventTime);
												}
											}
											bool flag8 = j < num3 - 1;
											if (flag8)
											{
												bool flag9 = array[j + 1] == 10;
												if (flag9)
												{
													num6 = j;
													j = num6 + 1;
												}
											}
											num4 = j + 1;
											num5 = 0;
											text = string.Empty;
											num = 0;
										}
										else
										{
											num6 = num5;
											num5 = num6 + 1;
										}
									}
									num6 = j;
								}

								bool flag10 = num5 > 0;
								if (flag10)
								{
									text += this.enckr.GetString(array, num4, num5);
									num += num5;
								}
								num2 += (long)num3;
							}
							bool flag11 = num > 0;
							if (flag11)
							{
								object eventTime2 = this.getEventTime(text);
								bool flag12 = eventTime2 != null;
								if (flag12)
								{
									this.addLogData(text, i, num2 - (long)num, num, (DateTime)eventTime2);
								}
							}
						}
						num6 = i;
					}
				}
			}
			catch (Exception ex)
			{
			}
			finally
			{
				Cursor.Current = Cursors.Default;
			}
		}

		//1.按行读取（全部）本地Log File -> 生成Log Data数据标签
		public void GenerateLogData(string useFTP, string logDate, string fromTime, string toTime, string localDir)
		{
			this.logCount = 0;
			try
			{
				//1.参数赋值 - PlayBack日期、起始时间、结束时间、LogFile文件夹
				this.m_LogDate = logDate;
				this.m_FromHour = Convert.ToInt32(fromTime);
				this.m_ToHour = Convert.ToInt32(toTime);
				this.m_LocalDir = localDir;

				this.logDataList.Clear();
				Cursor.Current = Cursors.WaitCursor;
				//2.获取Log File List
				this.getLocalLogFile(useFTP);

				if (this.localLogFileList.Count > 0)
				{
					//循环读取每个Log File -> 加载数据
					for (int i = 0; i < this.localLogFileList.Count; i++)
					{
						PlaybackManager.localLogFileInfo localLogFileInfo = (PlaybackManager.localLogFileInfo)this.localLogFileList[i];
						using (StreamReader fStream = new StreamReader(localLogFileInfo.filePath, UTF8Encoding.UTF8))
						{
							long point = 0;
							while (fStream.BaseStream.CanRead && !fStream.EndOfStream)
							{
								//2.将数据逐行读取填入数据队列
								string sLine = fStream.ReadLine();
								DateTime dt = (DateTime)getEventTime(sLine);
								int len = sLine.Length + 2;  //length+2 是因为 \r\n
								addLogData(i, point, len, dt);  //获取Log Data，只是Log Data的相关数据，不包含具体的数据

								point += len;

							}
							fStream.Close();
						}
					}
				}
			}
			catch (Exception ex)
			{
			}
			finally
			{
				Cursor.Current = Cursors.Default;
			}
		}

		//2.按行读取（单个小时）本地Log File -> 生成Log Data数据标签
		public void GenerateLogData(int curHour)
		{
			this.logCount = 0;
			try
			{
				this.logDataList.Clear();
				Cursor.Current = Cursors.WaitCursor;
				if (this.localLogFileList.Count > 0)
				{
					localLogFileInfo localLogFileInfo = this.localLogFileList.Find(k => k.m_Hour == curHour);
					if (localLogFileInfo != null)
					{
						using (StreamReader fStream = new StreamReader(localLogFileInfo.filePath, UTF8Encoding.UTF8))
						{
							long point = 0;
							while (fStream.BaseStream.CanRead && !fStream.EndOfStream)
							{
								//2.将数据逐行读取填入数据队列
								string sLine = fStream.ReadLine();
								DateTime time = (DateTime)getEventTime(sLine);
								int Offset = 0;
								for (int i = 0; i < sLine.Length; i++)
								{
									if ((int)sLine[i] > 127)                     //判断是否为中文，UFT8编码为3个字节
									{
										Offset += 2;
									}
								}
								int len = sLine.Length + Offset + 2;             //length+2 是因为 \r\n

								addLogData(curHour, point, len, time);  //Log Data - 只是Log Data的相关数据，不包含具体的数据	
								point += len;
							}
							fStream.Close();
						}
					}

				}
			}
			catch (Exception ex)
			{
				LogHelper.loggerError("获取回放日志数据时，在方法GenerateLogData中报错", ex);
			}
			finally
			{
				Cursor.Current = Cursors.Default;
			}
		}

		//3.分页加载对应文件中对应的Log Data
		public void GetXmlDatatPage(int page)
		{
			int pageIndex = (page - 1) * 20;
			for (int i = 0; i < 20; i++)
			{
				if (this.logCount > pageIndex + i)
				{
					this.setXmlData(i, this.logDataList[pageIndex + i], pageIndex + i);
				}
				else
				{
					DataRow dataRow = this.dtList.Rows[i];
					dataRow[0] = string.Empty;
					dataRow[1] = string.Empty;
					dataRow[2] = string.Empty;
				}
			}
		}

		//4.LogDataInfo是log Data相关信息，包括 - File Index，对应的File Data中的Postion Index、以及对应的数据长度
		//根据这些信息，重新去读取本地对应文件的File Data，获取Play Back String读取指定文件的PlayBack String读取 -> 存入DataTable（dtList）
		private void setXmlData(int rowPos, LogDataInfo info, int index)
		{
			try
			{
				bool flag = false;
				DataRow dataRow = this.dtList.Rows[rowPos];
				localLogFileInfo localLogFileInfo = this.localLogFileList.Find(k => k.m_Hour == info.fileHour);
				if (info.fileHour >= 0 && localLogFileInfo != null)
				{

					using (FileStream fileStream = File.Open(localLogFileInfo.filePath, FileMode.Open, FileAccess.Read))
					{
						if (fileStream != null)
						{
							fileStream.Seek(info.pos, SeekOrigin.Begin);
							int count = fileStream.Read(this.xmlbuf, 0, info.byteLen);
							string @string = this.enckr.GetString(this.xmlbuf, 0, count);
							if (@string.Length > 20)
							{
								dataRow[0] = index.ToString();
								dataRow[1] = @string.Substring(11, 12);
								dataRow[2] = @string.Substring(0);// (24);
								flag = true;
							}
						}
					}
				}

				if (!flag)
				{
					dataRow[0] = string.Empty;
					dataRow[1] = string.Empty;
					dataRow[2] = string.Empty;
				}
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.ToString());
			}
		}

		//5.Log File List Clear
		public void CloseLogFile()
		{
			if (this.localLogFileList.Count > 0)
			{
				this.localLogFileList.Clear();
			}
		}


		
		public void Cancel()
		{
			this.bCancel = true;
		}

	
		public string[] parseFileInfo(string line)
		{
			MatchCollection matchCollection = Regex.Matches(line, "[^ ]+");
			Match match = Regex.Match(line, "^([0-9]{2})-([0-9]{2})-([0-9]{2})");
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			long num4 = 0L;
			string text = string.Empty;
			string text2 = string.Empty;
			string text3 = "F";
			string[] array = null;
			bool flag = matchCollection.Count < 4;
			string[] result;
			if (flag)
			{
				result = null;
			}
			else
			{
				try
				{
					bool success = match.Success;
					if (success)
					{
						bool flag2 = match.Groups[3].Value[0] >= '8';
						if (flag2)
						{
							num = Convert.ToInt32("19" + match.Groups[3].Value);
						}
						else
						{
							num = Convert.ToInt32("20" + match.Groups[3].Value);
						}
						num2 = Convert.ToInt32(match.Groups[1].Value);
						num3 = Convert.ToInt32(match.Groups[2].Value);
						text = line.Substring(matchCollection[3].Index);
						bool flag3 = matchCollection[2].Value == "<DIR>";
						if (flag3)
						{
							text += "/";
							text3 = "D";
						}
						else
						{
							bool flag4 = long.TryParse(matchCollection[2].Value, out num4);
							if (flag4)
							{
								text2 = num4.ToString();
							}
						}
					}

					bool flag5 = matchCollection.Count > 5 && long.TryParse(matchCollection[4].Value, out num4);
					if (flag5)
					{
						text = line.Substring(matchCollection[8].Index);
						bool flag6 = matchCollection[0].Value[0] != 'd';
						if (flag6)
						{
							text2 = num4.ToString();
						}
						else
						{
							text3 = "D";
							bool flag7 = text[text.Length - 1] != '.';
							if (flag7)
							{
								text += "/";
							}
						}
						bool flag8 = matchCollection[7].Length == 4;
						if (flag8)
						{
							num = Convert.ToInt32(matchCollection[7].Value);
						}
						else
						{
							num = DateTime.Now.Year;
						}
						num2 = (int)Enum.Parse(typeof(PlaybackManager.MonthType), matchCollection[5].Value);
						num3 = Convert.ToInt32(matchCollection[6].Value);
					}
					array = new string[]
					{
						string.Format("{0:0000}-{1:00}-{2:00}", num, num2, num3),
						text2,
						text,
						text3,
						""
					};
				}
				catch (Exception ex)
				{
				}
				result = array;
			}
			return result;
		}
		private string[] parseHTTPFileInfo(string line)
		{
			string[] result = null;
			bool flag = !string.IsNullOrEmpty(line);
			if (flag)
			{
				result = new string[]
				{
					line.Replace("PlayBack-", "").Replace(".log", ""),
					line.Length.ToString(),
					line,
					"F",
					string.Empty
				};
			}
			return result;
		}
	
		private bool isDestFile(string useFTP, string fileName, out int hour)
		{
			bool result = false;
			int curHour = -1;
			if (useFTP == "Y")
			{
				bool flag2 = fileName.Contains(this.m_LogDate);
				if (flag2)
				{
					Match match = Regex.Match(fileName, "([0-9]{8})-([0-9]{2})");
					bool success = match.Success;
					if (success)
					{
						curHour = Convert.ToInt32(match.Groups[2].Value);
						bool flag3 = match.Groups[1].Value == this.m_LogDate && curHour >= this.m_FromHour && curHour <= this.m_ToHour;
						if (flag3)
						{
							result = true;
						}
					}
				}
			}
			else
			{
				string a = string.Empty;
				//截取到日期的文件名
				bool flag4 = fileName.Length > 19;
				if (flag4)
				{
					a = fileName.Substring(0, 19);
				}
				else
				{
					a = fileName;
				}
				bool flag5 = a == "PLAYBACK-" + this.m_LogDate;
				if (flag5)
				{
					Match match2 = Regex.Match(fileName, "([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2})");
					bool success2 = match2.Success;
					if (success2)
					{
						curHour = Convert.ToInt32(match2.Groups[4].Value);
						bool flag6 =  curHour >= this.m_FromHour && curHour <= this.m_ToHour;
						if (flag6)
						{
							result = true;
						}
					}
				}
			}
			hour = curHour;
			return result;
		}

		//获取对应文件夹下的满足时间筛选要求的文件List
		private void getLocalLogFile(string useFTP)
		{
			this.localLogFileList.Clear();
			bool flag = string.IsNullOrEmpty(this.m_LogDate);
			if (!flag)
			{
				DirectoryInfo directoryInfo = new DirectoryInfo(this.m_LocalDir);
				FileInfo[] files = directoryInfo.GetFiles("*" + this.m_LogDate + "*");
				FileInfo[] array = files;
				for (int i = 0; i < array.Length; i++)
				{
					FileInfo fileInfo = array[i];
					/*					if (this.isDestFile(useFTP, fileInfo.Name, out _))
										{

										}*/
					PlaybackManager.localLogFileInfo value = new PlaybackManager.localLogFileInfo(Path.Combine(this.m_LocalDir, fileInfo.Name));
					this.localLogFileList.Add(value);
				}
			}
		}

		//获取对应文件夹下的满足时间筛选要求的文件List（文件List需要填入对应的Hour时间）
		public void getLocalLogFile(string useFTP, string logDate, string fromTime, string toTime, string localDir)
		{
			//1.参数赋值 - PlayBack日期、起始时间、结束时间、LogFile文件夹
			this.m_LogDate = logDate;
			this.m_FromHour = Convert.ToInt32(fromTime);
			this.m_ToHour = Convert.ToInt32(toTime);
			this.m_LocalDir = localDir;

			this.localLogFileList.Clear();
			if (!string.IsNullOrEmpty(this.m_LogDate))
			{
				DirectoryInfo directoryInfo = new DirectoryInfo(this.m_LocalDir);
				FileInfo[] files = directoryInfo.GetFiles("*" + this.m_LogDate + "*"); //选定日期的全部Log File		
				for (int i = 0; i < files.Length; i++)
				{
					FileInfo fileInfo = files[i];

                    //筛选在指定时间范围内的文件
                    try
                    {
						int fileHour = Convert.ToInt32(fileInfo.Name.Substring(9, 2));
                        if (fileHour >= m_FromHour && fileHour <= m_ToHour)
                        {
							localLogFileInfo value = new localLogFileInfo(Path.Combine(this.m_LocalDir, fileInfo.Name));
							value.m_Hour = fileHour;
							this.localLogFileList.Add(value);
						}
                    }
                    catch (Exception ex)
                    {
						LogHelper.loggerError("在playback加载文件中报错，在getLocalLogFile方法中", ex);
                    }

					//2.满足Start/End时间的Log File
					//if (this.isDestFile(useFTP, fileInfo.Name, out int curHour))
					//{
					//	localLogFileInfo value = new localLogFileInfo(Path.Combine(this.m_LocalDir, fileInfo.Name));
					//	value.m_Hour = curHour;
					//	if (!DateTime.Now.Hour.ToString().Equals(curHour.ToString()))
					//	{
					//		this.localLogFileList.Add(value);
					//	}
					//}
				}
			}
		}


		private object getEventTime(string line)
		{
			bool flag = line != string.Empty && line.Length > 20;
			object result;
			if (flag)
			{
				DateTime now = DateTime.Now;
				string logTime = line.Substring(0, 19);
				bool flag2 = DateTime.TryParse(logTime, out now);
				if (flag2)
				{
					result = now;
					return result;
				}
			}
			result = null;
			return result;
		}
		private string getEventXml(string line)
		{
			bool flag = line != string.Empty && line.Length > 20;
			string result;
			if (flag)
			{
				int num = line.IndexOf("<MESSAGE");
				bool flag2 = num >= 0;
				if (flag2)
				{
					result = line.Substring(num);
					return result;
				}
			}
			result = string.Empty;
			return result;
		}


		private void InitialDataTable()
		{
			this.dtList = new DataTable();
			this.dtList.Columns.Add(new DataColumn("index"));
			this.dtList.Columns.Add(new DataColumn("datetime"));
			this.dtList.Columns.Add(new DataColumn("data"));
			string[] values = new string[]
			{
				"",
				"",
				""
			};
			int num;
			for (int i = 0; i < 20; i = num + 1)
			{
				this.dtList.Rows.Add(values);
				num = i;
			}
		}
		private void addLogData(string data, int fi, long pos, int cnt, DateTime dt)
		{
			//Match match = Regex.Match(data, "<MESSAGE.+</MESSAGE>");
			//bool success = match.Success;
			//if (success)
			//{
			PlaybackManager.LogDataInfo logDataInfo = new PlaybackManager.LogDataInfo
			{
				fileHour = fi,
				pos = pos,
				byteLen = cnt,
				time = dt
			};
			this.logDataList.Add(logDataInfo);
			this.logCount++;
			//}
		}


		private void addLogData(int fi, long pos, int cnt, DateTime dt)
		{
			PlaybackManager.LogDataInfo logDataInfo = new PlaybackManager.LogDataInfo
			{
				fileHour = fi,
				pos = pos,
				byteLen = cnt,
				time = dt
			};
			this.logDataList.Add(logDataInfo);
			this.logCount++;
		}

	

		//PlayBack Log读取测试
		//启动PlayBack后，多个文件循环读取并丢到stringQueue
		public void PlayingBackStrLoad(int index, string path)
		{
			//string path = "../../LOG/LogFile/1.log";
			Encoding encoding = UTF8Encoding.UTF8;

			//1. Check文件是否存在
			if (!File.Exists(path))
				return;

			using (StreamReader fStream = new StreamReader(path, encoding))
			{
				while (fStream.BaseStream.CanRead && !fStream.EndOfStream)
				{
					//2.将数据逐行读取填入数据队列
					string sLine = fStream.ReadLine();
					stringQueue.Enqueue(sLine);
				}
				fStream.Close();
			}
		}



		Dictionary<string, string[]> ObjectList_Old = new Dictionary<string, string[]>();  //用于对比的每个对象的play Back String
																						   //将SavePlayingBack String -> 对象（by时间点）
		public Dictionary<DateTime, string[]> GetPlayingBack(string data)
		{
			return null;
			//string FullString = data;
			//if (string.IsNullOrEmpty(FullString)) return null;

			////1.对象字符串分割(分割完成 -> 时间 + 各种类型对象)
			//string[] ObjectList = FullString.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
			//DateTime dt;
			//if (ObjectList == null || ObjectList.Length == 0) return null;
			//try
			//{
			//	dt = DateTime.ParseExact(ObjectList[0], "yyyy-MM-dd HH:mm:ss,fff", CultureInfo.InvariantCulture);
			//}
			//catch
			//{
			//	return null;
			//}

			//Comm_MultiDataReport resList = new Comm_MultiDataReport()
			//{
			//	ReplyIOData = new List<ApiObjectBase>(),
			//	CurrAlarmList = new List<Comm_AlarmDataReport>(),
			//	CurrCommandList = new List<Comm_CommandDataReport>()
			//};

			//#region compare to List and string[]
			////compare to List and string[]

			//lock (this.SyncObject)
			//{
			//	//2.遍历各种类型对象
			//	for (int i = 1; i < ObjectList.Length; i++)
			//	{
			//		int index = ObjectList[i].IndexOf('@');
			//		if (index == -1)
			//			continue;

			//		string reportName = ObjectList[i].Substring(0, index);                                  //获取每种类型对象名字
			//		string allData = ObjectList[i].Substring(index + 1, ObjectList[i].Length - index - 1);  //获取每种类型的所有对象String
			//		string[] reportData = allData.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);  //将当前类型对象拆分成单个对象String

			//		//3.必须发布1 - Comm_CommandDataReport
			//		if (reportName.ToLower().Contains("command"))
			//		{
			//			foreach (var single in reportData)
			//			{
			//				Comm_CommandDataReport commandObj = new Comm_CommandDataReport();
			//				UtilityService.SetPropertiesString(commandObj, 1, single);
			//				if (commandObj != null)
			//					resList.CurrCommandList.Add(commandObj);
			//			}
			//		}

			//		//4.必须发布2 - Comm_AlarmDataReport
			//		else if (reportName.ToLower().Contains("alarm"))
			//		{
			//			foreach (var single in reportData)
			//			{
			//				Comm_AlarmDataReport alarmObj = new Comm_AlarmDataReport();
			//				UtilityService.SetPropertiesString(alarmObj, 1, single);
			//				if (alarmObj != null)
			//					resList.CurrAlarmList.Add(alarmObj);
			//			}
			//		}

			//		//5.其他类型须对比后发布
			//		else
			//		{
			//			//1)有对比数据 + 保证对比数据与对象数据一致
			//			if (ObjectList_Old != null && ObjectList_Old.Count != 0 && ObjectList_Old.Count == ObjectList.Length - 2)
			//			{
			//				//保证有对象对比数据 + 对象数据长度一致
			//				if (!ObjectList_Old.ContainsKey(reportName) || ObjectList_Old[reportName].Length != reportData.Length)
			//					continue;

			//				string[] compareObject = ObjectList_Old[reportName];

			//				//1) Comm_MasterDataReport
			//				if (reportName.ToLower().Contains("master"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					for (int j = 0; j < reportData.Length; j++)
			//					{
			//						if (reportData[j] != compareObject[j])
			//						{
			//							Comm_MasterDataReport masterObj = new Comm_MasterDataReport()
			//							{
			//								ReadDataIO = new MasterReadCopy(),
			//								WriteDataIO = new MasterWriteCopy(),
			//								CommList = new List<EqpConnectInform>()
			//							};
			//							UtilityService.SetPropertiesString(masterObj, 1, reportData[j]);
			//							if (masterObj != null)
			//								resList.ReplyIOData.Add(masterObj);
			//						}
			//					}
			//				}
			//				//2) Comm_CraneDataReport
			//				else if (reportName.ToLower().Contains("crane"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					for (int j = 0; j < reportData.Length; j++)
			//					{
			//						if (reportData[j] != compareObject[j])
			//						{
			//							Comm_CraneDataReport craneObj = new Comm_CraneDataReport()
			//							{
			//								ReadDataIO = new CraneReadCopy(),
			//								WriteDataIO = new CraneWriteCopy()
			//							};
			//							UtilityService.SetPropertiesString(craneObj, 1, reportData[j]);
			//							if (craneObj != null)
			//								resList.ReplyIOData.Add(craneObj);
			//						}
			//					}
			//				}

			//				//3）Comm_CVPortDataReport
			//				else if (reportName.ToLower().Contains("cvport"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					for (int j = 0; j < reportData.Length; j++)
			//					{
			//						if (reportData[j] != compareObject[j])
			//						{
			//							Comm_CVPortDataReport cvPortObj = new Comm_CVPortDataReport()
			//							{
			//								ReadDataIO = new CVPortReadCopy(),
			//								WriteDataIO = new CVPortWriteCopy()
			//							};
			//							UtilityService.SetPropertiesString(cvPortObj, 1, reportData[j]);
			//							if (cvPortObj != null)
			//								resList.ReplyIOData.Add(cvPortObj);
			//						}
			//					}
			//				}

			//				//4) Comm_InlinePortDataReport
			//				else if (reportName.ToLower().Contains("inlineport"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					for (int j = 0; j < reportData.Length; j++)
			//					{
			//						if (reportData[j] != compareObject[j])
			//						{
			//							Comm_InlinePortDataReport inlinePortObj = new Comm_InlinePortDataReport()
			//							{
			//								ReadDataIO = new InlinePortReadCopy(),
			//								WriteDataIO = new InlinePortWriteCopy()
			//							};
			//							UtilityService.SetPropertiesString(inlinePortObj, 1, reportData[j]);
			//							if (inlinePortObj != null)
			//								resList.ReplyIOData.Add(inlinePortObj);
			//						}
			//					}

			//				}

			//				//5) Comm_ShelfDataReport
			//				else if (reportName.ToLower().Contains("shelf"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					for (int j = 0; j < reportData.Length; j++)
			//					{
			//						if (reportData[j] != compareObject[j])
			//						{
			//							Comm_ShelfDataReport shelfObj = new Comm_ShelfDataReport();
			//							UtilityService.SetPropertiesString(shelfObj, 1, reportData[j]);
			//							if (shelfObj != null)
			//								resList.ReplyIOData.Add(shelfObj);
			//						}
			//					}

			//				}



			//				//6) Comm_NCPDataReport
			//				else if (reportName.ToLower().Contains("ncp"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					for (int j = 0; j < reportData.Length; j++)
			//					{
			//						if (reportData[j] != compareObject[j])
			//						{
			//							Comm_NCPDataReport ncpObj = new Comm_NCPDataReport();
			//							UtilityService.SetPropertiesString(ncpObj, 1, reportData[j]);
			//							if (ncpObj != null)
			//								resList.ReplyIOData.Add(ncpObj);
			//						}
			//					}

			//				}

			//				//7) Comm_CarrierDataReport
			//				else if (reportName.ToLower().Contains("carrier"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					for (int j = 0; j < reportData.Length; j++)
			//					{
			//						if (reportData[j] != compareObject[j])
			//						{
			//							Comm_CarrierDataReport carrierObj = new Comm_CarrierDataReport();
			//							UtilityService.SetPropertiesString(carrierObj, 1, reportData[j]);
			//							if (carrierObj != null)
			//								resList.ReplyIOData.Add(carrierObj);
			//						}
			//					}
			//				}
			//			}

			//			//2)第一笔对象数据
			//			else if (ObjectList[i] != null)
			//			{
			//				//1) Comm_MasterDataReport
			//				if (reportName.ToLower().Contains("master"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					foreach (var single in reportData)
			//					{
			//						Comm_MasterDataReport masterObj = new Comm_MasterDataReport()
			//						{
			//							ReadDataIO = new MasterReadCopy(),
			//							WriteDataIO = new MasterWriteCopy(),
			//							CommList = new List<EqpConnectInform>()
			//						};
			//						UtilityService.SetPropertiesString(masterObj, 1, single);
			//						if (masterObj != null)
			//							resList.ReplyIOData.Add(masterObj);

			//					}
			//				}

			//				//2) Comm_CraneDataReport
			//				else if (reportName.ToLower().Contains("crane"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					foreach (var single in reportData)
			//					{
			//						Comm_CraneDataReport craneObj = new Comm_CraneDataReport()
			//						{
			//							ReadDataIO = new CraneReadCopy(),
			//							WriteDataIO = new CraneWriteCopy()
			//						};
			//						UtilityService.SetPropertiesString(craneObj, 1, single);
			//						if (craneObj != null)
			//							resList.ReplyIOData.Add(craneObj);
			//					}
			//				}

			//				//3）Comm_CVPortDataReport
			//				else if (reportName.ToLower().Contains("cvport"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					foreach (var single in reportData)
			//					{
			//						Comm_CVPortDataReport cvPortObj = new Comm_CVPortDataReport()
			//						{
			//							ReadDataIO = new CVPortReadCopy(),
			//							WriteDataIO = new CVPortWriteCopy()
			//						};
			//						UtilityService.SetPropertiesString(cvPortObj, 1, single);
			//						if (cvPortObj != null)
			//							resList.ReplyIOData.Add(cvPortObj);
			//					}
			//				}

			//				//4) Comm_InlinePortDataReport
			//				else if (reportName.ToLower().Contains("inlineport"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					foreach (var single in reportData)
			//					{
			//						Comm_InlinePortDataReport inlinePortObj = new Comm_InlinePortDataReport()
			//						{
			//							ReadDataIO = new InlinePortReadCopy(),
			//							WriteDataIO = new InlinePortWriteCopy()
			//						};
			//						UtilityService.SetPropertiesString(inlinePortObj, 1, single);
			//						if (inlinePortObj != null)
			//							resList.ReplyIOData.Add(inlinePortObj);
			//					}
			//				}

			//				//5) Comm_ShelfDataReport
			//				else if (reportName.ToLower().Contains("shelf"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					foreach (var single in reportData)
			//					{
			//						Comm_ShelfDataReport shelfObj = new Comm_ShelfDataReport();
			//						UtilityService.SetPropertiesString(shelfObj, 1, single);
			//						if (shelfObj != null)
			//							resList.ReplyIOData.Add(shelfObj);
			//					}
			//				}

			//				//6) Comm_NCPDataReport
			//				else if (reportName.ToLower().Contains("ncp"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					foreach (var single in reportData)
			//					{
			//						Comm_NCPDataReport ncpObj = new Comm_NCPDataReport();
			//						UtilityService.SetPropertiesString(ncpObj, 1, single);
			//						if (ncpObj != null)
			//							resList.ReplyIOData.Add(ncpObj);
			//					}
			//				}

			//				//7) Comm_CarrierDataReport
			//				else if (reportName.ToLower().Contains("carrier"))
			//				{
			//					//将每个对象进行对比，并确定是否发布
			//					foreach (var single in reportData)
			//					{
			//						Comm_CarrierDataReport carrierObj = new Comm_CarrierDataReport();
			//						UtilityService.SetPropertiesString(carrierObj, 1, single);
			//						if (carrierObj != null)
			//							resList.ReplyIOData.Add(carrierObj);
			//					}
			//				}
			//			}
			//		}


			//		ObjectList_Old[reportName] = reportData;
			//	}

			//	#region 20220420 Disable By ZhangCai
			//	//List<ApiObjectBase> resList = new List<ApiObjectBase>();
			//	//for (int i = 1; i < ObjectList.Length; i++)
			//	//{
			//	//    int index = ObjectList[i].IndexOf('@');
			//	//    if (index == -1) continue;
			//	//    string reportName = ObjectList[i].Substring(0, index);
			//	//    string reportData = ObjectList[i].Substring(index + 1, ObjectList[i].Length - index - 1);
			//	//    //1.Comm_MasterDataReport
			//	//    if (reportName.ToLower().Contains("master"))
			//	//    {
			//	//        Comm_MasterDataReport masterObj = new Comm_MasterDataReport()
			//	//        {
			//	//            ReadDataIO = new MasterReadCopy(),
			//	//            WriteDataIO = new MasterWriteCopy(),
			//	//            CommList = new List<EqpConnectInform>()
			//	//        };

			//	//        UtilityService.SetPropertiesString(masterObj, 1, reportData);
			//	//        resList.Add(masterObj);
			//	//    }
			//	//    //2.Comm_CraneDataReport
			//	//    else if (reportName.ToLower().Contains("crane"))
			//	//    {
			//	//        Comm_CraneDataReport craneObj = new Comm_CraneDataReport()
			//	//        {
			//	//            ReadDataIO = new CraneReadCopy(),
			//	//            WriteDataIO = new CraneWriteCopy()
			//	//        };

			//	//        UtilityService.SetPropertiesString(craneObj, 1, reportData);
			//	//        resList.Add(craneObj);
			//	//    }

			//	//    //3.Comm_CVPortDataReport
			//	//    else if (reportName.ToLower().Contains("cvport"))
			//	//    {
			//	//        Comm_CVPortDataReport cvPortObj = new Comm_CVPortDataReport()
			//	//        {
			//	//            ReadDataIO = new CVPortReadCopy(),
			//	//            WriteDataIO = new CVPortWriteCopy()
			//	//        };

			//	//        UtilityService.SetPropertiesString(cvPortObj, 1, reportData);
			//	//        resList.Add(cvPortObj);
			//	//    }

			//	//    //4.Comm_InlinePortDataReport
			//	//    else if (reportName.ToLower().Contains("inlineport"))
			//	//    {
			//	//        Comm_InlinePortDataReport inlinePortObj = new Comm_InlinePortDataReport()
			//	//        {
			//	//            ReadDataIO = new InlinePortReadCopy(),
			//	//            WriteDataIO = new InlinePortWriteCopy()
			//	//        };

			//	//        UtilityService.SetPropertiesString(inlinePortObj, 1, reportData);
			//	//        resList.Add(inlinePortObj);
			//	//    }

			//	//    //5.Comm_ShelfDataReport
			//	//    else if (reportName.ToLower().Contains("shelf"))
			//	//    {
			//	//        Comm_ShelfDataReport shelfObj = new Comm_ShelfDataReport();
			//	//        UtilityService.SetPropertiesString(shelfObj, 1, reportData);
			//	//        resList.Add(shelfObj);
			//	//    }

			//	//    //6.Comm_CommandDataReport
			//	//    else if (reportName.ToLower().Contains("command"))
			//	//    {
			//	//        Comm_CommandDataReport commandObj = new Comm_CommandDataReport();
			//	//        UtilityService.SetPropertiesString(commandObj, 1, reportData);
			//	//        resList.Add(commandObj);
			//	//    }

			//	//    //7.Comm_NCPDataReport
			//	//    else if (reportName.ToLower().Contains("ncp"))
			//	//    {
			//	//        Comm_NCPDataReport ncpObj = new Comm_NCPDataReport();
			//	//        UtilityService.SetPropertiesString(ncpObj, 1, reportData);
			//	//        resList.Add(ncpObj);
			//	//    }

			//	//    //8.Comm_CarrierDataReport
			//	//    else if (reportName.ToLower().Contains("carrier"))
			//	//    {
			//	//        Comm_CarrierDataReport carrierObj = new Comm_CarrierDataReport();
			//	//        UtilityService.SetPropertiesString(carrierObj, 1, reportData);
			//	//        resList.Add(carrierObj);
			//	//    }

			//	//    //9.Comm_AlarmDataReport
			//	//    else if (reportName.ToLower().Contains("alarm"))
			//	//    {
			//	//        Comm_AlarmDataReport commandObj = new Comm_AlarmDataReport();
			//	//        UtilityService.SetPropertiesString(commandObj, 1, reportData);
			//	//        resList.Add(commandObj);
			//	//    }



			//	//}
			//	#endregion

			//	return new Dictionary<DateTime, Comm_MultiDataReport>() { { dt, resList } };
			//}
			//#endregion
		}

		public void ClearOldObjectList()
		{
			lock (this.SyncObject)
			{
				ObjectList_Old.Clear();
			}
		}
	}
}
